package com.yinshan.voiceplay;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;

import com.yinshan.voiceplay.utils.MoneyUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author limh
 * @function 语音转文本工具
 * @date 2019/1/4 11:54
 */
public class PlayUtils {
    private static final String TAG = "PlayUtils";
    private static PlayUtils instance;

    private Context context;
    private List<Integer> voices;
    private MediaPlayer mediaPlayer;
    private boolean isPlaying = false;

    private boolean isPlaying() {
        return isPlaying;
    }

    private void setPlaying(boolean playing) {
        isPlaying = playing;
    }

    private PlayUtils(Context context) {
        this.context = context.getApplicationContext();
    }

    public static PlayUtils getInstance(Context context) {
        if (null == instance) {
            instance = new PlayUtils(context);
        }
        return instance;
    }

    public PlayUtils addVoice(String voiceString) {
        if (null == voices) {
            voices = new ArrayList<>();
        }
        voices.clear();
        //‘收款成功’语音索引
        voices.add(R.raw.tts_success);
        String money = MoneyUtils.getMoney(voiceString);
        if (TextUtils.isEmpty(money)) {
            return this;
        }
        //金额转换为语音索引
        if (money.contains(".")) {
            String[] tmp = money.split("\\.");
            voices.addAll(MoneyUtils.readIntPart(tmp[0]));
            if (!MoneyUtils.readDecimalPart(tmp[1]).isEmpty()) {
                voices.add(R.raw.tts_dot);
                voices.addAll(MoneyUtils.readDecimalPart(tmp[1]));
            }
        } else {
            voices.addAll(MoneyUtils.readIntPart(money));
        }
        //‘元’语音索引
        voices.add(R.raw.tts_yuan);
        return this;
    }

    public void startPlay() {
        if (isPlaying()) return;
        setPlaying(true);
        playList(0, voices.size());
    }

    private void playList(final int index, final int length) {
        if (null == voices || voices.isEmpty()) {
            Log.d(TAG, "");
            return;
        }
        mediaPlayer = MediaPlayer.create(context, voices.get(index));
        try {
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mp.release();
                    int newsoundindex = index;
                    if (index < length - 1) {
                        newsoundindex = newsoundindex + 1;
                        playList(newsoundindex, length);
                    } else {
                        setPlaying(false);
                    }
                }
            });
            mediaPlayer.start();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.setPlaybackParams(mediaPlayer.getPlaybackParams().setSpeed(1.5f));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
